package com.fosu.interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fosu.common.ResponseResult;
import com.fosu.dao.UserDao;
import com.fosu.domain.Product;
import com.fosu.domain.User;
import com.fosu.Utils.ThreadLocal2JsonObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @author zqa
 * @version 1.0
 * 发布拦截器
 * @date 2020/11/13 18:01
 */
@Component
public class PublishInterceptor implements HandlerInterceptor {

    @Autowired
    UserDao userDao;


    /**
     * 在请求前处理
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        ServletInputStream inputStream = request.getInputStream();//获取requset中的字节流
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);//读取字节并将它们解码为字符   字节-》字符
        BufferedReader reader = new BufferedReader(inputStreamReader);//从字符输入流中读取文本并缓冲字符
        String str = "";
        String wholeStr = "";
        JSONObject jsonObject = null;
        Product product = null;

        User user = null;
        Integer userId = null;

        while ((str = reader.readLine()) != null) {  //读取reader中数据 直到为null
            wholeStr += str;//赋值给wholeStr
        }
        try {
            jsonObject = JSONObject.parseObject(wholeStr);//转换为Json对象
            ThreadLocal2JsonObjectUtil.setJSONObject(jsonObject);//将json存入jsonObject
            product = jsonObject.getObject("product", Product.class);
            userId = product.getUserId();
            user = userDao.findUserById(userId);

            if (user.getWechatId() == null || user.getWechatId().equals("")) {
                response.setCharacterEncoding("utf-8");
                response.getWriter().write(JSON.toJSONString(ResponseResult.fail(response, "球球你写下微信好吧 ┭┮﹏┭┮")));
                return false;
            }


                System.out.println("拦截器test");

        } catch (Exception e) {
            response.setCharacterEncoding("utf-8");
            response.getWriter().write(JSON.toJSONString(ResponseResult.fail(response, "发布拦截器内部错误")));
            e.printStackTrace();
        } finally {
            reader.close();
            System.out.println("关了流");
        }

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ThreadLocal2JsonObjectUtil.removeThreadLocal();
        System.out.println("关了啊");
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }


}
